package com.two.lbms.hjx.service;

import com.two.lbms.base.BaseService;
import com.two.lbms.dao.AdminMapper;
import com.two.lbms.dao.DictionaryListMapper;
import com.two.lbms.dao.IllegalListMapper;
import com.two.lbms.dao.ReaderMapper;
import com.two.lbms.hjx.dto.IllegalDTO;
import com.two.lbms.hjx.model.IllegalQuery;
import com.two.lbms.utils.AssertUtil;
import com.two.lbms.vo.DictionaryList;
import com.two.lbms.vo.IllegalList;
import com.two.lbms.vo.Reader;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author hjx
 * @version V1.0
 * @Date 2023/7/31 19:51
 */
@Service
@Slf4j
public class IllegalService extends BaseService<IllegalList,Integer> {
    @Resource
    private IllegalListMapper illegalListMapper;
    @Resource
    private DictionaryListMapper dictionaryListMapper;
    @Resource
    private ReaderMapper readerMapper;

    /**
     * 添加违规记录
     * @param illegalQuery 违规记录
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void addIllegal(IllegalQuery illegalQuery) {

        AssertUtil.isTrue(StringUtils.isBlank(illegalQuery.getDictionaryValue()),"违规类型不能为空");
        AssertUtil.isTrue(StringUtils.isBlank(illegalQuery.getReaderName()),"借阅人不能为空");
        DictionaryList dictionaryList = dictionaryListMapper.selectByDictionaryValue("违规类型",illegalQuery.getDictionaryValue());
        Reader reader = readerMapper.selectByReaderName(illegalQuery.getReaderName());
        IllegalList illegalList = new IllegalList();
        illegalList.setIllegalType(dictionaryList.getId());
        illegalList.setReaderId(reader.getId());
        illegalList.setAdminName(illegalQuery.getAdminName());
        illegalList.setIllegalContent(illegalQuery.getIllegalContent());
        illegalList.setIllegalDate(LocalDateTime.now());
        illegalList.setCreateTime(LocalDateTime.now());
        illegalList.setUpdateTime(LocalDateTime.now());
        illegalList.setIsValid(1);
        AssertUtil.isTrue(illegalListMapper.insert(illegalList)<1,"添加失败");
        //违规次数加1
        reader.setViolationTimes(reader.getViolationTimes() + 1);
        AssertUtil.isTrue(readerMapper.updateByPrimaryKeySelective(reader)<1,"更新违规次数失败");

        log.info("添加成功，id : {}", illegalList.getId());

    }

    /**
     * 根据id查询违规记录
     * @param id 违规记录id
     * @return 违规记录
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public IllegalDTO selectByIllegalId(Integer id) {
        return illegalListMapper.selectById(id);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void updateIllegal(IllegalQuery illegalQuery) {
        log.info("违规修改前端数据 : {}",illegalQuery);
        AssertUtil.isTrue(StringUtils.isBlank(illegalQuery.getDictionaryValue()),"违规类型不能为空");
        AssertUtil.isTrue(StringUtils.isBlank(illegalQuery.getReaderName()),"违规人不能为空");

        DictionaryList dictionaryList = dictionaryListMapper.selectByDictionaryValue("违规类型",illegalQuery.getDictionaryValue());
        Reader reader = readerMapper.selectByReaderName(illegalQuery.getReaderName());

        IllegalList illegalList = new IllegalList();
        illegalList.setId(illegalQuery.getId());
        illegalList.setIllegalType(dictionaryList.getId());
        illegalList.setIllegalContent(illegalQuery.getIllegalContent());
        illegalList.setReaderId(reader.getId());
        illegalList.setAdminName(illegalQuery.getAdminName());
        illegalList.setUpdateTime(LocalDateTime.now());
        illegalListMapper.updateByPrimaryKeySelective(illegalList);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteIllegal(Integer id) {
        log.info("要删除的id : {}",id);
        AssertUtil.isTrue(id == null,"id不能为空");
        IllegalList illegalList = new IllegalList();
        illegalList.setId(id);
        illegalList.setUpdateTime(LocalDateTime.now());
        illegalList.setIsValid(0);
        AssertUtil.isTrue(illegalListMapper.updateByPrimaryKeySelective(illegalList) < 1,"删除失败");
    }
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteIllegalBatch(Integer[] ids) {
        AssertUtil.isTrue(ids == null || ids.length == 0,"要删除的id不能为空");
        log.info("要删除的id : {}", Arrays.toString(ids));
        List<IllegalList> list = new ArrayList<>();
        for (Integer id : ids) {
            IllegalList illegalList = new IllegalList();
            illegalList.setId(id);
            illegalList.setIsValid(0);
            illegalList.setUpdateTime(LocalDateTime.now());
            list.add(illegalList);
        }
        AssertUtil.isTrue(illegalListMapper.updateBatch(list) != ids.length,"删除失败");;
    }
}
